package wx.utils.gzh;

import com.jinyou.utils.common.ValidateUtil;
import net.sf.json.JSONObject;
import wx.dao.AccessToken;
import wx.utils.WeixinUtil;

import java.util.HashMap;

//微信公众号
public class WXXcxMan {
    public static boolean pushXcxhMsg(String appId, String appSecret,
                                      String toUser, String template_id,
                                      String url, String topcolor,
                                      HashMap xcxMap,
                                      HashMap dataMap) {
        return pushXcxhMsg(appId, appSecret,
                toUser, template_id,
                url, topcolor,
                xcxMap,
                dataMap, null);
    }

    public static boolean pushXcxhMsg(String appId, String appSecret,
                                      String toUser, String template_id,
                                      String url, String topcolor,
                                      HashMap xcxMap,
                                      HashMap dataMap,
                                      String formId) {
        // 调用接口获取access_token
        AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);
        HashMap map = new HashMap();
        map.put("touser", toUser);
        map.put("template_id", template_id);
        map.put("page", "pages/index-new/index-new");

        if (ValidateUtil.isNotNull(url)) {
            map.put("url", url);
        }
        if (null != xcxMap) {
            map.put("miniprogram", xcxMap);
        }

        if (ValidateUtil.isNotNull(formId)) {
            map.put("form_id", formId);
        }


        if (ValidateUtil.isNotNull(topcolor)) {
            map.put("topcolor", topcolor);
        }
        map.put("data", dataMap);

        return WeixinUtil.sendXcxTemplateMsg(at.getToken(), JSONObject.fromObject(map).toString()) == 0 ? true : false;
    }
}
